package com.woniuxy.microfundservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.microfundservice.bean.FundMsg;
import com.woniuxy.microfundservice.bean.FundNavigation;
import com.woniuxy.microfundservice.bean.info.*;
import com.woniuxy.microfundservice.service.FundFullInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * (FundFullInfo)表控制层
 *
 * @author makejava
 * @since 2021-04-07 12:00:41
 */
@RefreshScope
@Slf4j
@RestController
@RequestMapping(value = "/fund", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "基金相关接口")
public class FundFullInfoController {
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private FundFullInfoService fundFullInfoService;


    //单个添加基金
    @ApiOperation("添加单个基金")
    @PostMapping("/addone")
    public ResultBean addOne (@RequestBody FundFullInfo fundFullInfo) {
        int res = fundFullInfoService.addOne(fundFullInfo);
        return ResultBean.builder().code(200).msg("新增成功").build();
    }

    //单个添加基金
    @ApiOperation("根据id查找基金")
    @GetMapping("/selectById")
    public ResultBean selectById (@RequestParam("id") int id ) {
        FundFullInfo fundFullInfo= fundFullInfoService.selectById(id);
        return ResultBean.builder().code(200).msg("查找成功").data(fundFullInfo).build();
    }


    //添加多个基金
    @PostMapping("/addmulti")
    @ApiOperation("批量新增")
    public ResultBean addMulti (List<FundFullInfo> fundFullInfos) {
        int res = fundFullInfoService.addMulti(fundFullInfos);
        return ResultBean.builder().code(200).msg("新增成功").build();
    }

    @GetMapping("/selectkey")
    @ApiOperation("查找基金")
    public ResultBean selectKey (String select) {
        List<FundFullInfo> fundFullInfos = fundFullInfoService.selectKey(select);
        return ResultBean.builder().code(200).data(fundFullInfos).build();
    }

    @GetMapping("/selectall")
    @ApiOperation("查找所有基金")
    public ResultBean selectAll (int page, int limit) {
        List<FundFullInfo> fundFullInfos = fundFullInfoService.selectAll();
        //添加分页
        PageHelper.startPage(page, limit);
        PageInfo<FundFullInfo> fundFullInfoPageInfo = new PageInfo<>(fundFullInfos);

        return ResultBean.builder().code(200).data(fundFullInfoPageInfo).build();
    }

    //从接口查询数据
    @GetMapping("/selectnet")
    @ApiOperation("从阿里接口查基金数据并保存")
    public ResultBean selectNet (@RequestParam("fundCodeInt") Integer fundCodeInt) {
        String fundCode=String.format("%06d",fundCodeInt);
        return fundFullInfoService.selectNet (fundCode);
    }

    @PutMapping("/updateone")
    @ApiOperation("更改基金数据")
    public ResultBean updateOne (@RequestBody FundFullInfo fundFullInfo) {
        int res = fundFullInfoService.updateOne(fundFullInfo);
        return ResultBean.builder().code(200).msg("success").build();
    }

    @PostMapping("/delete")
    @ApiOperation("下架指定基金")
    public ResultBean delete (@RequestBody List<Integer> id) {
        int res = fundFullInfoService.delete(id);
        return ResultBean.builder().code(200).msg("下架成功").build();
    }

    //根据投资策略筛选基金
    @ApiOperation("根据投资策略筛选基金")
    @GetMapping("/policy")
    public ResultBean selectBYPolicy (String policy) {
        List<FundFullInfo> policies = fundFullInfoService.selectBYPolicy(policy);
        return ResultBean.builder().code(200).data(policies).build();
    }

    @GetMapping("/hot")
    @ApiOperation("获取热门基金")
    public ResultBean getHot () {
        String url = "https://api.doctorxiong.club/v1/fund/hot";
        HotFundMsg h = restTemplate.getForObject(url, HotFundMsg.class);
        List<HotFund> data = h.getData().getRank();

        return ResultBean.builder().code(200).data(data).build();
    }

    @GetMapping("/selectOne")
    @ApiOperation("获取基金详情")
    public ResultBean selectOne (int id) {
        //获取基金信息
        FundMsg fundMsg = fundFullInfoService.selectOne(id);
        return ResultBean.builder().code(200).data(fundMsg).build();
    }

    @GetMapping("/selectdelete")
    @ApiOperation("查找所有下架基金")
    public ResultBean selectDelete (int page, int limit) {
        List<FundFullInfo> fundFullInfos = fundFullInfoService.selectDelete();
        //添加分页
        PageHelper.startPage(page, limit);
        PageInfo<FundFullInfo> fundFullInfoPageInfo = new PageInfo<>(fundFullInfos);

        return ResultBean.builder().code(200).data(fundFullInfoPageInfo).build();
    }

    @GetMapping("/selectkeydel")
    @ApiOperation("搜索下架基金")
    public ResultBean selectKeyDelete (String select) {
        List<FundFullInfo> fundFullInfos = fundFullInfoService.selectKeyDelete(select);
        return ResultBean.builder().code(200).data(fundFullInfos).build();
    }

    @PostMapping("/deleteDel")
    @ApiOperation("彻底删除选中的下架基金")
    public ResultBean deleteDel (@RequestBody List<Integer> id) {
        int res = fundFullInfoService.deleteDel(id);
        return ResultBean.builder().code(200).msg("删除成功").build();
    }

    @GetMapping("/recoverfund")
    @ApiOperation("恢复下架基金")
    public ResultBean recoverFund (int id) {
        int res = fundFullInfoService.recoverFund(id);
        return ResultBean.builder().code(200).msg("恢复成功").build();
    }

    @GetMapping("/showNavigation")
    @ApiOperation("查询基金导航")
    public ResultBean showNavigation(){
        List<FundNavigation> fundNavigations = fundFullInfoService.showNavigation();
        return ResultBean.builder().code(200).msg("查询基金导航成功").data(fundNavigations).build();
    }
}